/**
 * 购物车管理器 - 本地存储
 */

class CartManager {
    constructor() {
        this.storageKey = 'shopping_cart'
    }

    // 获取购物车数据
    getCartData() {
        try {
            const data = uni.getStorageSync(this.storageKey)
            return data || { items: [], totalCount: 0, totalPrice: 0 }
        } catch (error) {
            return { items: [], totalCount: 0, totalPrice: 0 }
        }
    }

    // 保存购物车数据
    saveCartData(cartData) {
        try {
            uni.setStorageSync(this.storageKey, cartData)
            return true
        } catch (error) {
            return false
        }
    }

    // 添加商品
    addItem(goodsInfo, quantity = 1) {
        const cartData = this.getCartData()
        
        // 查找是否已存在
        const existingIndex = cartData.items.findIndex(item => item.goodsId === goodsInfo.id)
        
        if (existingIndex !== -1) {
            // 已存在，增加数量
            cartData.items[existingIndex].quantity += quantity
        } else {
            // 新商品
            cartData.items.push({
                id: Date.now(),
                goodsId: goodsInfo.id,
                name: goodsInfo.name,
                price: goodsInfo.price,
                quantity: quantity,
                image: goodsInfo.image,
                selected: true
            })
        }
        
        this.updateSummary(cartData)
        this.saveCartData(cartData)
        return true
    }

    // 删除商品
    removeItem(goodsId) {
        const cartData = this.getCartData()
        cartData.items = cartData.items.filter(item => item.goodsId !== goodsId)
        this.updateSummary(cartData)
        this.saveCartData(cartData)
        return true
    }

    // 更新数量
    updateQuantity(goodsId, quantity) {
        if (quantity <= 0) {
            return this.removeItem(goodsId)
        }
        
        const cartData = this.getCartData()
        const item = cartData.items.find(item => item.goodsId === goodsId)
        
        if (item) {
            item.quantity = quantity
            this.updateSummary(cartData)
            this.saveCartData(cartData)
        }
        
        return true
    }

    // 清空购物车
    clearCart() {
        const emptyData = { items: [], totalCount: 0, totalPrice: 0 }
        this.saveCartData(emptyData)
        return true
    }

    // 更新汇总信息
    updateSummary(cartData) {
        cartData.totalCount = cartData.items.reduce((sum, item) => sum + item.quantity, 0)
        cartData.totalPrice = cartData.items
            .filter(item => item.selected)
            .reduce((sum, item) => sum + (item.price * item.quantity), 0)
    }

    // 切换选中状态
    toggleSelection(goodsId, selected) {
        const cartData = this.getCartData()
        const item = cartData.items.find(item => item.goodsId === goodsId)
        
        if (item) {
            item.selected = selected
            this.updateSummary(cartData)
            this.saveCartData(cartData)
        }
    }

    // 全选/取消全选
    selectAll(selected) {
        const cartData = this.getCartData()
        cartData.items.forEach(item => {
            item.selected = selected
        })
        this.updateSummary(cartData)
        this.saveCartData(cartData)
    }
}

export default new CartManager() 